package com.xhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhu.constant.SessionConstant;
import com.xhu.exceptions.CompeteException;
import com.xhu.mapper.CompeteMapper;
import com.xhu.pojo.Compete;
import com.xhu.service.CompeteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class CompeteServiceImpl extends ServiceImpl<CompeteMapper, Compete> implements CompeteService {

    private static final String PATH = "D:\\crmsystem\\compete";

    @Autowired
    CompeteMapper mapper;

    @Override
    public IPage<Compete> getAllToPage(int current, int size) {
        if(current<1||size<1){
            throw new CompeteException("查询失败");
        }

        return mapper.selectPage(new Page<>(current, size), null);
    }

    @Override
    public IPage<Compete> getAllByMonthToPage(int current, int size, String month) {
        LambdaQueryWrapper<Compete> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Compete::getCompeteMonth, month);

        return mapper.selectPage(new Page<>(current, size), wrapper);
    }

    @Override
    public void updateOneById(Compete entity) {
        if (entity == null) {
            throw new CompeteException("插入失败");
        }
        if (entity.getId() == null) {
            throw new CompeteException("插入失败");
        }
        //防止主动修改
        entity.setIsDeleted(null)
                .setUpdateTime(null);

        int flag = mapper.updateById(entity);
        if (flag != 1) {
            throw new CompeteException("更新失败");
        }
    }

    @Override
    public void insertOne(Compete entity) {
        if (entity == null) {
            throw new CompeteException("插入失败");
        }

        //防止主动修改
        entity.setIsDeleted(null)
                .setUpdateTime(null);

        int res = mapper.insert(entity);
        if (res != 1) {
            throw new CompeteException("插入失败");
        }
    }

    @Override
    public void uplordFile(MultipartFile file, HttpServletRequest request) {
        if (file.isEmpty()) {
            throw new CompeteException("文件为空");
        }
        File uploadrDir = new File(PATH);
        if (!uploadrDir.exists()) {
            uploadrDir.mkdir();
        }

        String originFileName = file.getOriginalFilename();
        //判空
        if (!StringUtils.hasText(originFileName)) {
            throw new CompeteException("文件名字为空");
        }
        //获取后缀
        String suffixName = originFileName.substring(originFileName.lastIndexOf("."));
        //随机生成名字
        String fileanme = UUID.randomUUID() + suffixName;
        File localFile = new File(PATH, fileanme);
        //移动文件到本地目录
        try {
            file.transferTo(localFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CompeteException("上传失败");
        }

        request.getSession().setAttribute(SessionConstant.COMPETE_FILEPATH, fileanme);

    }

    @Override
    public void guidang(Integer id){
        LambdaUpdateWrapper<Compete> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Compete::getCompeteStatus,"已归档")
                .eq(Compete::getId,id);
        int flag = mapper.update(new Compete().setId(id), wrapper);
        if (flag != 1) {
            throw new CompeteException("归档失败");
        }
    }



}
